package com.jmpt.fangbohui.ui;

import org.json.JSONObject;

import android.R.bool;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jmpt.fangbohui.Constants;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.AgentListFilterBean;
import com.jmpt.fangbohui.bean.NewHouseFilterBean;
import com.jmpt.fangbohui.bean.OldHouseFilterBean;
import com.jmpt.fangbohui.bean.RentHouseFilterBean;
import com.jmpt.fangbohui.http.ApiRequest;
import com.jmpt.fangbohui.http.JsonHttpHandler;
import com.jmpt.fangbohui.ui.base.BaseHeadActivity;
import com.jmpt.fangbohui.utils.FileUtils;
import com.jmpt.fangbohui.utils.SpUtils;
import com.jmpt.fangbohui.utils.ToastHelper;
import com.jmpt.fangbohui.utils.UIUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class SelectCityActivity extends BaseHeadActivity {
	
	private int flag = 0;
	
	private ProgressDialog mProgressDialog;
	
	private boolean rentHouse;
	private boolean oldHouse;
	private boolean newHouse;
	private boolean agent;
	
	private boolean isRentHouseFinish;
	private boolean isOldHouseFinish;
	private boolean isNewHouseFinish;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		showTitle("选择城市");
		
		showBackButton(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		setContentView(R.layout.activity_select_city);
		
		ViewUtils.inject(this);
		
		 mProgressDialog = new ProgressDialog(this);
         mProgressDialog.setMessage(mContext.getString(R.string.message_loading));
         mProgressDialog.setCanceledOnTouchOutside(false);
         mProgressDialog.setCancelable(false);
		
	}
	
	@OnClick(R.id.tv_city_jiangmen)
	public void jiangmen(View view){
		mProgressDialog.show();
		
		addRequestHandle(ApiRequest.getRentHouseFilter(1 , new JsonHttpHandler(this){
			
			@Override
			public void onDo(JSONObject response) {
				try {
					Gson g = new Gson();
					g.fromJson(response.toString(), RentHouseFilterBean.class);
					FileUtils.write2JsonCache(Constants.SharedPreferences.RENT_HOUSE_FILTER, response.toString());
					rentHouse = true;
				} catch (Exception e) {
					e.printStackTrace();
					ToastHelper.ShowToast("城市数据获取失败", UIUtils.getContext());
				}
			}
			@Override
			public void onFinish() {
				super.onFinish();
				getNewHouseFilter();
			}
		}));
		
	}

	public void getNewHouseFilter() {
		addRequestHandle(ApiRequest.getNewHouseFilter(1 , new JsonHttpHandler(this){
			@Override
			public void onDo(JSONObject response) {
				try {
					Gson g = new Gson();
					g.fromJson(response.toString(), NewHouseFilterBean.class);
					FileUtils.write2JsonCache(Constants.SharedPreferences.NEW_HOUSE_FILTER, response.toString());
					newHouse = true;
				} catch (Exception e) {
					e.printStackTrace();
					ToastHelper.ShowToast("城市数据获取失败", UIUtils.getContext());
				}
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				getOldHouseFilter();
			}
		}));
	}

	private void getOldHouseFilter() {
		addRequestHandle(ApiRequest.getOldHouseFilter(1 , new JsonHttpHandler(this){
			@Override
			public void onDo(JSONObject response) {
				try {
					Gson g = new Gson();
					g.fromJson(response.toString(), OldHouseFilterBean.class);
					FileUtils.write2JsonCache(Constants.SharedPreferences.OLD_HOUSE_FILTER, response.toString());
					oldHouse = true;
				} catch (Exception e) {
					e.printStackTrace();
					ToastHelper.ShowToast("城市数据获取失败", UIUtils.getContext());
				}
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				getAgentListFilter();
			}
		}));
	}
	
	private void getAgentListFilter() {
		addRequestHandle(ApiRequest.getAgentListFilter(1 , new JsonHttpHandler(this){
			@Override
			public void onDo(JSONObject response) {
				try {
					Gson g = new Gson();
					g.fromJson(response.toString(), AgentListFilterBean.class);
					FileUtils.write2JsonCache(Constants.SharedPreferences.AGENT_LIST_FILTER, response.toString());
					agent = true;
				} catch (Exception e) {
					e.printStackTrace();
					ToastHelper.ShowToast("城市数据获取失败", UIUtils.getContext());
				}
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				if(mProgressDialog != null && mProgressDialog.isShowing()){
					mProgressDialog.dismiss();
				}
				checkFinish();
			}
		}));
	}
	
	protected void checkFinish() {
		if(oldHouse && newHouse && rentHouse && agent){
			finish();
		}
	}

}
